package com.example.config;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientImpl;
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

@Configuration
public class DockerConfig {

    @Bean
    public DefaultDockerClientConfig defaultDockerClientConfig() {
//        DockerClientConfig custom = DefaultDockerClientConfig.createDefaultConfigBuilder()
//                .withDockerHost("tcp://docker.somewhere.tld:2376")
//                .withDockerTlsVerify(true)
//                .withDockerCertPath("/home/user/.docker")
//                .withRegistryUsername(registryUser)
//                .withRegistryPassword(registryPass)
//                .withRegistryEmail(registryMail)
//                .withRegistryUrl(registryUrl)
//                .build();
        // 可以使用这种形式进行本地测试
        return DefaultDockerClientConfig.createDefaultConfigBuilder().build();
    }

    @Bean
    public ApacheDockerHttpClient apacheDockerHttpClient() {
        return new ApacheDockerHttpClient.Builder()
                .dockerHost(defaultDockerClientConfig().getDockerHost())
                .sslConfig(defaultDockerClientConfig().getSSLConfig())
                .maxConnections(100)
                .connectionTimeout(Duration.ofSeconds(30))
                .responseTimeout(Duration.ofSeconds(45))
                .build();
    }

    @Bean
    public DockerClient dockerClient() {
        return DockerClientImpl.getInstance(defaultDockerClientConfig(), apacheDockerHttpClient());
    }
}
